package com.timoon.sample.web.product;

import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.spring.injection.annot.SpringBean;

import com.googlecode.wicket.jquery.ui.Options;
import com.timoon.app.utils.Constantor;
import com.timoon.app.utils.HttpServletUtil;
import com.timoon.component.panel.jqgrid.JqGridPanel;
import com.timoon.component.panel.jqgrid.JqGridService;
import com.timoon.component.panel.jqgrid.panel.CreatorPanel;
import com.timoon.component.panel.jqgrid.panel.DetailsPanel;
import com.timoon.component.panel.nav.ResourceMapper;
import com.timoon.sample.web.product.panel.ProductCreatorPanel;
import com.timoon.sample.web.product.panel.ProductDetailPanel;
import com.timoon.sample.web.user.JqGridServiceImpl;

/**
 *<p></p>
 *<p></p>
 *<p>Copyright:Copyright(c)2013.4.1</p>
 *<p>Company:timoon</p>
 *@author 付乐天
 *@version 1.0 */
public class ProductGirdPanel extends Panel {

	@SpringBean(name="com.timoon.webcomponent.utility.product.service.ProductService")
	private JqGridService jqGridServiceImpl;
	
	public ProductGirdPanel(String id) {
		super(id);
		// TODO Auto-generated constructor stub
		
		
		renderGrid();
	}

	private void renderGrid() {
		// TODO Auto-generated method stub
		
		Options ops=new Options();
		//ops.set("url", "'"+HttpServletUtil.getBasePath()+"/controller/jqgrid/getGridData'");
		ops.set("colModel", "[" +
				"{name:'id',index:'id',label:'ID'}," +
				"{name:'name',index:'name',label:'商品名'}," +
				"{name:'market_price',index:'market_price',label:'市场价'}," +
				"{name:'actual_price',index:'actual_price',label:'实际价'}," +
				"{name:'rate',index:'rate',label:'评分等级'}," +
				"{name:'status',index:'status',label:'状态'}" +
				"]");
		
		add(new JqGridPanel("wk_product",ops,jqGridServiceImpl,null) {
			
			@Override
			public boolean isButtonVisible(String buttonName) {
				// TODO Auto-generated method stub
				ResourceMapper mapper=(ResourceMapper) this.getSession().getAttribute(Constantor.SESSION_USER_MAPPER);
				boolean visible=false;
				if(buttonName.equals(JqGridPanel.ADD_BUTTON_ENABEL)){
					return mapper.getResourceMap().containsKey("product_add");
				}else if(buttonName.equals(JqGridPanel.DELETE_BUTTON_ENABLE)){
					return mapper.getResourceMap().containsKey("product_delete");
				}else if(buttonName.equals(JqGridPanel.MODIFIER_BUTTON_ENABLE)){
					return mapper.getResourceMap().containsKey("product_modifier");
				}else if(buttonName.equals(JqGridPanel.DETAIL_BUTTON_ENABLE)){
					return mapper.getResourceMap().containsKey("product_detail");
				}
				
				return visible;
			}
			
			@Override
			public DetailsPanel getDetailsPanel(String id,
					JqGridPanel mainPanel, String recordId) {
				// TODO Auto-generated method stub
				return new ProductDetailPanel(id, mainPanel, recordId);
			}
			
			@Override
			public CreatorPanel getCreatorPanel(String id,
					JqGridPanel mainPanel, String recordId) {
				// TODO Auto-generated method stub
				return new ProductCreatorPanel(id, mainPanel, recordId);
			}
		});
	}
	


}
